#!/bin/bash

# Test script for connect command with feedthrough modules
# Tests the -v (verbose) flag to show wire names

set -e

echo "=================================================="
echo "Test: Connect command with feedthrough modules"
echo "Testing -v (verbose) flag to display wire names"
echo "=================================================="

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Build the project
echo -e "\n${BLUE}Step 1: Building the project...${NC}"
cd /Users/wenyiduan/gitee/modb/moparse-rs
cargo build --release
echo -e "${GREEN}✓ Build completed${NC}"

# Set binary path
MO_BIN="./target/release/mo"

# Test file
TEST_FILE="tests/test_fd_path.v"
MODB_FILE="test_fd_path.modb"

# Clean up old files
rm -f "$MODB_FILE"

# Parse the test file
echo -e "\n${BLUE}Step 2: Parsing Verilog file...${NC}"
$MO_BIN parse -v "$TEST_FILE" -o "$MODB_FILE"
echo -e "${GREEN}✓ Parsing completed${NC}"

# Show parsed modules
echo -e "\n${BLUE}Step 3: Showing parsed modules...${NC}"
$MO_BIN show -i "$MODB_FILE"

# Test 1: From u_module_a through u_fd1, u_fd2 to u_module_b (without verbose)
echo -e "\n${YELLOW}=================================================${NC}"
echo -e "${YELLOW}Test 1: Signal path from u_module_a -> u_fd1 -> u_fd2 -> u_module_b (Normal mode)${NC}"
echo -e "${YELLOW}=================================================${NC}"
$MO_BIN connect -i "$MODB_FILE" -m top --from u_module_a --fd u_fd1 --fd u_fd2 --to u_module_b

# Test 2: Same path with verbose flag
echo -e "\n${YELLOW}=================================================${NC}"
echo -e "${YELLOW}Test 2: Signal path from u_module_a -> u_fd1 -> u_fd2 -> u_module_b (Verbose mode)${NC}"
echo -e "${YELLOW}Expected: Wire names should be displayed under each connection${NC}"
echo -e "${YELLOW}=================================================${NC}"
$MO_BIN connect -i "$MODB_FILE" -m top --from u_module_a --fd u_fd1 --fd u_fd2 --to u_module_b -v

# Test 3: From top through feedthrough modules
echo -e "\n${YELLOW}=================================================${NC}"
echo -e "${YELLOW}Test 3: Signal path from top -> u_fd1 -> u_fd2 (Verbose mode)${NC}"
echo -e "${YELLOW}=================================================${NC}"
$MO_BIN connect -i "$MODB_FILE" -m top --from-top --fd u_fd1 --fd u_fd2 -v

# Test 4: Through feedthrough modules to top
echo -e "\n${YELLOW}=================================================${NC}"
echo -e "${YELLOW}Test 4: Signal path from u_fd1 -> u_fd2 -> top (Verbose mode)${NC}"
echo -e "${YELLOW}=================================================${NC}"
$MO_BIN connect -i "$MODB_FILE" -m top --from u_fd1 --fd u_fd2 --to-top -v

# Test 5: Single feedthrough
echo -e "\n${YELLOW}=================================================${NC}"
echo -e "${YELLOW}Test 5: Signal path from u_module_a -> u_fd1 -> u_module_b (Verbose mode)${NC}"
echo -e "${YELLOW}=================================================${NC}"
$MO_BIN connect -i "$MODB_FILE" -m top --from u_module_a --fd u_fd1 --to u_module_b -v

echo -e "\n${GREEN}=================================================${NC}"
echo -e "${GREEN}All tests completed successfully!${NC}"
echo -e "${GREEN}=================================================${NC}"
echo ""
echo "The -v flag should display wire names like:"
echo "  \\_ wire_a_to_fd1"
echo "  \\_ wire_fd1_to_fd2"
echo "  \\_ wire_fd2_to_b"
echo ""
echo "Cleanup: Remove test database file with:"
echo "  rm $MODB_FILE"
